<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
/**
 * HITI AdminCP
 *
 * @package     HITI AdminCP Project
 * @since       Monday, October 21, 2013, 6:00 PM
 * @final       
 * @category    Models
 * @see 		User logs
 * @author      PHUOCNGUYEN - HITI Dev Team
 * @copyright   Copyright (c) 2013, HITI Corp
 */

class Userlogs_model extends CI_Model {
	public function __construct()
	{
		parent::__construct();
	}

	public function show($uri, $num_rows = false)
	{
		$this->db->query('SET NAMES "LATIN1"');
		if ($uri['limit'] == '') $uri['limit'] = 50;
		$this->db->from('userlogs AS l');
		if ($uri['user'] != 1)
		{
			$this->db->where('user', $uri['user']);
		}
		if ($uri['q'] != '')
		{
			$this->db->like('log', $uri['q']);
			$this->db->or_like('log_val', $uri['q']);
		}
		if ($uri['from'] != '')
		{
			$this->db->where(sprintf('l.date_added >= %1$s', $this->db->escape($uri['from'].' 00:00:00')));
		}
		if ($uri['to'] != '')
		{
			$this->db->where(sprintf('l.date_added <= %1$s', $this->db->escape($uri['to'].' 23:59:59')));
		}
		if ($num_rows)
		{
			$this->db->select('id');
			return $this->db->get()->num_rows();
		}
		else
		{
			$last_login = $GLOBALS['user']['last_login'];
			$this->db->select('l.*, u.username AS name, IF ('.sprintf('l.date_added >= %1$s', $this->db->escape($last_login)).', 1, 0) as isnew', false);
			$this->db->join('users AS u', 'l.user = u.id', 'left');
			$this->db->order_by('date_added DESC');
			$this->db->limit($uri['limit'], $uri['rowstart']);
			$query = $this->db->get();
			if ($query->num_rows() > 0)
			{
				return $query->result_array();
			}
			else
			{
				return false;
			}
		}
	}
}

/* End of file userlogs_model.php */
/* Location: ./application/models/userlogs_model.php */
